/**
 * 查看订单明细
 * 
 * @param {}
 *            orderId
 * @return {}
 */
var goodsPriceSizeForm = function(goodsId, goodsName, goodsCode) {
	var goodsPriceSizeForm = new Ext.form.FormPanel({
				id : 'goodsPriceSizeForm',
				anchor : '100% 10%',
				title : ' 查询条件',
				frame : true,
				bodyBorder : false,
				labelAlign : 'right',
				items : [{
							layout : 'column',
							border : false,
							items : [{
										columnWidth : .25,
										layout : 'form',
										border : false,
										items : [{
													xtype : 'displayfield',
													anchor : '100%',
													ctCls : 'zhaospaceCSS3',
													fieldLabel : '商品编号',
													value : goodsId

												}]
									}, {
										columnWidth : .25,
										layout : 'form',
										border : false,
										items : [{
													xtype : 'displayfield',
													anchor : '100%',
													name : "goodsName",
													ctCls : 'zhaospaceCSS3',
													fieldLabel : '商品名称',
													value : goodsName
												}]
									}, {
										columnWidth : .25,
										layout : 'form',
										border : false,
										items : [{
													xtype : 'displayfield',
													anchor : '100%',
													name : "goodsCode",
													ctCls : 'zhaospaceCSS3',
													fieldLabel : '商品编码',
													value : goodsCode
												}]
									}]
						}]
			});
	fieldSetListeners(['goodsPriceSizeForm', 'goodsPriceSizeGrid'],
			[0.10, 0.90]);
	return goodsPriceSizeForm;
};

/**
 * 商品尺寸
 * 
 * @param {}
 *            contractId
 * @return {}
 */
var goodsPriceSizeGrid = function(goodsId, goodsCode) {

	var column = new Ext.grid.ColumnModel({
		columns : [new Ext.grid.RowNumberer({
							width : 35
						}), {
					dataIndex : "id",
					header : '尺寸编号',
					width : 60
				}, {
					header : '商品规格编码',
					dataIndex : "goodsStandardCode",
					width : 100
				}, {
					header : "商品图片",
					dataIndex : "goodsPic",
					width : 80,
					renderer : function(value) {
						return PmsUtil.ShowPicture(value);
					}
				}, {
					header : '商品尺寸',
					dataIndex : "goodsSize",
					width : 70
				}, {
					header : "商品重量",
					dataIndex : "goodsWeight",
					width : 60,
					renderer : function(value, meta, record) {
						if (value) {
							meta.attr = 'ext:qtip="' + value + '"';
							return value;
						}
					}
				}, {
					dataIndex : "goodsUnits",
					header : "商品单位",
					sortable : true,
					width : 60
				}, {
					header : "商品单价",
					dataIndex : "goodsPrice",
					width : 60,
					renderer : function(val) {
						return PmsUtil.Price(val);
					}
				}, {
					dataIndex : "goodsEatNumber",
					header : "适合人数",
					sortable : true,
					width : 60
				},{
					dataIndex : "giveLaidNumber",
					header : "赠送餐具数量",
					sortable : true,
					width : 100
				}, {
					header : "排序",
					dataIndex : "goodsOrder",
					width : 50
				}, {
					header : "删除状态",
					dataIndex : "isDelete",
					width : 60,
					renderer : function(value) {
						return PmsUtil.IsDafault(value);
					}
				}, {
					header : '创建时间',
					width : 130,
					dataIndex : 'createdDate'
				}, {
					header : "创建人",
					dataIndex : "createdName",
					width : 60
				}, {
					header : '修改时间',
					dataIndex : "updatedDate",
					width : 130
				}, {
					header : '修改人',
					dataIndex : "updatedName",
					width : 60
				}, {
					header : "管理",
					width : 150,
					dataIndex : "id",
					sortable : false,
					renderer : function(j, i, g, m, h) {
						var l = g.data.id;
						var isDelete = g.data.isDelete;
						var isDefault = g.data.isDefault;

						var k = "";
						if (isGranted("goodsPriceSize_修改")) {
							if (isDelete == 0) {
								k += '&nbsp;&nbsp;<a href="#" onclick="updPriceSize('
										+ l + ');" >修改</a>';
							}
						}
						if (isGranted("goodsPriceSize_删除")) {
							if (isDelete == 0) {
								k += '&nbsp;&nbsp;<a href="#" onclick="updIsDelete('
										+ l + ',1);" >删除</a>';
							}
						}
						return k;
					}
				}],
		defaults : {
			remoteSort : false,
			menuDisabled : false,
			width : 100
		}
	});

	// 定义grid的toolbar
	var goodsPriceSizeTb = new Ext.Toolbar({
				id : "goodsPriceSizeTb",
				height : 25,
				items : []
			});

	goodsPriceSizeTb.add(new Ext.scm.Button({
				text : "添加尺寸",
				width : 80,
				iconCls : "btn-add",
				hsValue : 'goodsPriceSize_添加尺寸',
				pressed : true,
				listeners : {
					"click" : function() {
						addPriceSize(goodsId, goodsCode);
					}
				}
			}));
	goodsPriceSizeTb.add(new Ext.Toolbar.Spacer());
	goodsPriceSizeTb.add(new Ext.scm.Button({
				text : "刷 新",
				iconCls : "btn-refresh",
				width : 80,
				hsValue : 'goodsPriceSize_刷新',
				pressed : true,
				handler : function() {
					queryGoodsPriceSize();
				}
			}));

	var goodsPriceSizeStore = new Ext.data.Store({
				autoLoad : true,// 要加载数据必须设置为true
				proxy : new Ext.data.HttpProxy({
							method : 'POST',
							url : __ctxPath
									+ "/goodsManager/queryGoodsPriceSizeByGoodsId"
						}),
				baseParams : {
					goodsId : goodsId
				},
				reader : new Ext.data.JsonReader({
							totalProperty : "totalCount",
							root : "list",
							fields : [{
										name : "id"
									}, {
										name : "goodId"
									}, {
										name : "goodsCode"
									}, {
										name : "goodsStandardCode"
									}, {
										name : "goodsSize"
									}, {
										name : "goodsWeight"
									}, {
										name : "goodsPrice"
									}, {
										name : "goodsUnits"
									}, {
										name : "goodsOrder"
									}, {
										name : "isDelete"
									}, {
										name : "createdDate"
									}, {
										name : "createdName"
									}, {
										name : "updatedDate"
									}, {
										name : "updatedName"
									}, {
										name : "goodsPic"
									}, {
										name : "giveLaidNumber"
									},{
										name : "goodsEatNumber"
									}]

						})
			});
	var goodsPSGrid = new Ext.grid.GridPanel({
				id : 'goodsPriceSizeGrid',
				title : '商品尺寸信息',
				anchor : '100% 90%',
				store : goodsPriceSizeStore,
				stripeRows : true,
				autoScroll : true,
				columnLines : true,
				cm : column,
				loadMask : true,
				tbar : goodsPriceSizeTb,
				loadMask : {
					msg : "正在加载数据，请稍侯……"
				},
				viewConfig : {
					emptyText : '没有您想要的数据'
				},
				bbar : new PG.PagingBar({
							id : 'goodsPriceSizePage',
							store : goodsPriceSizeStore
						})
			});
	return goodsPSGrid;
};

// 查询
function queryGoodsPriceSize() {
	var form = Ext.getCmp("goodsPriceSizeForm");
	if (form.getForm().isValid()) {
		var grid = Ext.getCmp("goodsPriceSizeGrid");
		var store = grid.store;
		var pagesize = Ext.getCmp('goodsPriceSizePage').pageSize;
		store.load({
					params : {
						start : 0,
						limit : pagesize
					}
				});
	}
}

// 删除
function updIsDelete(id, isDelete) {
	Ext.Msg.confirm("信息确认", "是否删除该条商品尺寸信息?", function(btn) {
				if (btn == "yes") {
					var optionMask = new Ext.LoadMask(Ext.getBody(), {
								msg : "正在处理,请稍后..."
							});
					optionMask.show();
					Ext.Ajax.request({
								url : __ctxPath
										+ "/goodsManager/updateIsDelete",
								params : {
									id : id,
									isDelete : isDelete
								},
								method : "post",
								success : function(response) {
									optionMask.hide();
									var jsonData = Ext.util.JSON
											.decode(response.responseText);
									if (jsonData.success) {
										Ext.MessageBox.show({
													title : "信息提示",
													msg : jsonData.data,
													buttons : Ext.MessageBox.OK,
													icon : Ext.MessageBox.INFO
												});
										queryGoodsPriceSize();
									} else {
										Ext.MessageBox.show({
													title : "错误提示",
													msg : jsonData.data,
													buttons : Ext.MessageBox.OK,
													icon : Ext.MessageBox.ERROR
												});
									}
								},
								failure : function(response) {
									optionMask.hide();
									Ext.MessageBox.show({
												title : "操作信息",
												msg : "服务器请求异常,请稍后再试!",
												buttons : Ext.MessageBox.OK,
												icon : Ext.MessageBox.ERROR
											});
								}
							});
				}
			});
}

// 添加
function addPriceSize(goodsId, goodsCode) {
	try {
		addOrUpdGoodsPriceSize(null, "添加尺寸", goodsId, goodsCode).show();
	} catch (Error) {
		Ext.Loader.load([__ctxPath + '/js/goods/addOrUpdGoodsSize.js'],
				function() {
					addOrUpdGoodsPriceSize(null, "添加尺寸", goodsId, goodsCode)
							.show();
				});
	}
}

// 修改
function updPriceSize(id) {
	try {
		addOrUpdGoodsPriceSize(id, "修改尺寸").show();
	} catch (Error) {
		Ext.Loader.load([__ctxPath + '/js/goods/addOrUpdGoodsSize.js'],
				function() {
					addOrUpdGoodsPriceSize(id, "修改尺寸").show();
				});
	}
}
